package com.mzy.linear_struct.hot100.链表;

import jdk.nashorn.internal.ir.WhileNode;

import java.security.Key;
import java.util.*;

/**
 * @Author: mizy3
 * @Date: 2024/07/22/上午10:09
 * @Description:
 */
public class Pro146LRU缓存 {

    public static void main(String[] args) {
        LRUCache1 lRUCache = new LRUCache1(2);
        lRUCache.put(1, 1); // 缓存是 {1=1}
        lRUCache.put(2, 2); // 缓存是 {1=1, 2=2}
        System.out.println(lRUCache.get(1));    // 返回 1
        lRUCache.put(3, 3); // 该操作会使得关键字 2 作废，缓存是 {1=1, 3=3}
        System.out.println(lRUCache.get(2));    // 返回 -1 (未找到)
        lRUCache.put(4, 4); // 该操作会使得关键字 1 作废，缓存是 {4=4, 3=3}
        System.out.println(lRUCache.get(1));    // 返回 -1 (未找到)
        System.out.println(lRUCache.get(3));    // 返回 3
        System.out.println(lRUCache.get(4));    // 返回 4

        // LRUCache lruCache = new LRUCache(1);
        // lruCache.put(2, 1);
        // System.out.println(lruCache.get(2));

        LRUCache1 cache1 = new LRUCache1(2);
        cache1.put(2, 1);
        cache1.put(2, 2);
        System.out.println(cache1.get(2));
        cache1.put(1, 1);
        cache1.put(4, 1);
        System.out.println(cache1.get(2));
    }

}

/*class LRUCache { // 超时
    private LinkedList<Integer> cache;
    private HashMap<Integer, Integer> room;
    private final int capacity;
    private int size;

    public LRUCache(int capacity) {
        cache = new LinkedList<>();
        room = new HashMap<>();
        this.capacity = capacity;
        size = 0;
    }

    public int get(int key) {
        // 将当前key的tuple放到最前面
        if (room.containsKey(key)) {
            cache.remove(Integer.valueOf(key));
            cache.addFirst(key);
            return room.get(key);
        }
        return -1;
    }

    public void put(int key, int value) {
        if (size < capacity || cache.contains(key)) {
            if(!cache.contains(key)){
                size++;
            }
            cache.remove(Integer.valueOf(key));
            cache.addFirst(key);
            room.put(key, value);
        } else {
            Integer removeKey = cache.removeLast();
            room.remove(removeKey);
            cache.addFirst(key);
            room.put(key, value);
        }
    }
}*/

/*
class LRUCache1 {

    class Node {
        private Node prev;
        private Node next;
        int key, value;

        public Node(int key, int value) {
            this.key = key;
            this.value = value;
        }
    }

    private HashMap<Integer, Node> cache;
    private Node head, tail;
    private int capacity;

    public LRUCache(int capacity) {
        cache = new HashMap<>(capacity);
        head = new Node(-1, -1);
        tail = new Node(-1, -1);
        this.capacity = capacity;
        head.next = tail;
        tail.prev = head;
    }

    public int get(int key) {

        // 根据key去map中获取对应的node，
        // 然后将node从双向链表中删除
        // 再添加到双向链表的头部
        if (cache.containsKey(key)) {
            // 获取key对应的节点
            Node node = cache.get(key);
            // 删除链表
            delete(node);
            // 将获取的节点添加到头部
            addHead(node);
            return node.value;
        }

        return -1;
    }

    private void addHead(Node node) {
        node.next = head.next;
        node.prev = head;
        head.next.prev = node;
        head.next = node;
    }

    private void delete(Node node) {
        node.next.prev = node.prev;
        node.prev.next = node.next;
    }

    private Node removeLast() {
        Node last = tail.prev;
        last.prev.next = tail;
        tail.prev = last.prev;
        return last;
    }

    public void put(int key, int value) {
        // 查找当前key在cache中是否存在，存在则代表是更新
        Node newNode = new Node(key, value);

        if (cache.containsKey(key)) {
            // 获取node，并且删除node
            Node node = cache.get(key);
            cache.remove(node.key);
            delete(node);
        } else {
            // 如果大于容量的话，添加新的，删除最老的
            if (cache.size() >= capacity) {
                Node removeNode = removeLast();
                cache.remove(removeNode.key);
                // 把node节点存到双向链表的头部
            }
        }
        // 将node重新添加到头部
        addHead(newNode);
        cache.put(key, newNode);
    }
}
*/
/*

class LRUCache {

    private final Node head;
    private final Node tail;
    private final Map<Integer, Node> cache = new HashMap<>();

    class Node {
        private Node prev, next;
        private int key, value;

        public Node(int key, int value) {
            this.key = key;
            this.value = value;
            prev = next = null;
        }
    }

    private int size;
    private final int capacity;

    public LRUCache(int capacity) {
        this.capacity = capacity;
        head = new Node(-1, -1);
        tail = new Node(-1, -1);
        head.next = tail;
        tail.prev = head;
    }

    public int get(int key) {
        if (!cache.containsKey(key)) {
            return -1;
        }

        Node node = cache.get(key);
        removeNode(node);
        addHead(node);
        return node.value;
    }

    public void put(int key, int value) {
        Node node = new Node(key, value);

        // 判断是更新还是添加
        if (cache.containsKey(key)) { // 更新
            Node updateNode = cache.remove(key);
            removeNode(updateNode);
            addHead(node);
            cache.put(key, node);
        } else {
            // 添加
            // 判断容量是否满了
            if (size < capacity) { // 没满
                addHead(node);
                cache.put(key, node);
                size++;
            } else { // 满了，删了最老的，添加新的到头部
                Node last = removeLast();
                cache.remove(last.key);
                addHead(node);
                cache.put(key, node);
            }
        }

    }

    private void removeNode(Node node) {
        Node prev = node.prev;
        Node next = node.next;
        prev.next = next;
        next.prev = prev;
    }

    // h->a->b->t
    private Node removeLast() {
        Node last = tail.prev;
        last.prev.next = tail;
        tail.prev = last.prev;

        return last;
    }

    // h->t
    // h->a->t
    private void addHead(Node node) {
        node.next = head.next;
        node.prev = head;
        head.next.prev = node;
        head.next = node;
    }

    public static void main(String[] args) {
        LRUCache lruCache = new LRUCache(2);
        // lruCache.put(1, 1);
        // lruCache.put(2, 2);
        // System.out.println(lruCache.get(1));
        // lruCache.put(3,3);
        // System.out.println(lruCache.get(2));
        //

        lruCache.put(2, 1);
        System.out.println(lruCache.get(2));

    }
}

*/

/*
class LRUCache1 {
    class Node {
        int key, val;
        Node prev, next;

        public Node(int key, int val) {
            this.key = key;
            this.val = val;
        }
    }

    Map<Integer, Node> cache = new HashMap<>();
    Node head, tail;
    int size, capacity;

    public LRUCache1(int capacity) {
        this.capacity = capacity;
        head = new Node(-1, -1);
        tail = new Node(-1, -1);
        head.next = tail;
        tail.prev = head;
    }

    public void addHead(Node node) {
        Node next = head.next;
        head.next = node;
        node.prev = head;
        node.next = next;
        next.prev = node;
    }

    public Node removeLast() {
        Node last = tail.prev;
        last.prev.next = tail;
        tail.prev = last.prev;
        return last;
    }

    public Node remove(int key) {
        Node node = cache.get(key);
        Node prev = node.prev;
        Node next = node.next;
        prev.next = next;
        next.prev = prev;

        return node;
    }


    public void put(int key, int val) {
        Node node = new Node(key, val);
        // 更新还是添加
        if (cache.containsKey(key)) {
            // 更新
            remove(key);
            addHead(node);
            cache.remove(key);
            cache.put(key, node);
        } else {
            // 添加
            // 是否满了
            if (size >= capacity) {
                // 满了
                Node last = removeLast();
                addHead(node);
                cache.remove(last.key);
                cache.put(key, node);
            } else {
                addHead(node);
                cache.put(key, node);
                size++;
            }
        }
    }

    public int get(int key) {
        Node node = cache.get(key);
        if (node == null) {
            return -1;
        }
        remove(key);
        addHead(node);
        return node.val;
    }
}*/

class LRUCache1 {
    class Node {
        Node next, prev;
        int key, value;

        public Node(int key, int value) {
            this.key = key;
            this.value = value;
        }
    }

    Map<Integer, Node> cache = new HashMap<>();
    Node head, tail;
    int size, capacity;

    public LRUCache1(int capacity) {
        this.capacity = capacity;
        head = new Node(-1, -1);
        tail = new Node(-1, -1);
        head.next = tail;
        tail.prev = head;
    }

    public void addHead(Node node) {
        Node next = head.next;
        node.next = next;
        node.prev = head;
        next.prev = node;
        head.next = node;
    }

    public Node removeLast() {
        Node last = tail.prev;
        tail.prev = last.prev;
        last.prev.next = tail;
        return last;
    }

    public void remove(int key) {
        Node node = cache.get(key);
        if (node == null) return;

        Node next = node.next;
        Node prev = node.prev;
        prev.next = next;
        next.prev = prev;
    }

    public void put(int key, int value) {
        Node node = new Node(key, value);
        if (cache.containsKey(key)) {
            remove(key);
            addHead(node);
            cache.remove(key);
            cache.put(key, node);
        } else {

            if (size >= capacity) {
                Node last = removeLast();
                cache.remove(last.key);
                addHead(node);
                cache.put(key, node);
            } else {
                addHead(node);
                cache.put(key, node);
                size++;
            }
        }
    }

    public int get(int key) {
        Node node = cache.get(key);
        if (node == null) return -1;

        remove(key);
        addHead(node);
        return node.value;
    }


}